package com.eightywork.life.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.eightywork.life.R;
import com.eightywork.life.activity.BrowserActivity;
import com.eightywork.life.activity.ContentActivity;
import com.eightywork.life.activity.IndexActivity;
import com.eightywork.life.activity.SpaceActivity;
import com.eightywork.life.imageCache.ImageLoader;
import com.eightywork.life.model.Notice;
import com.eightywork.life.service.BaseService;
import com.eightywork.life.service.DataService;

public class NoticeListAdapter extends ArrayAdapter<Notice>{
	
	private LayoutInflater mInflater;
	private ImageLoader imageLoader;
	private Context n_context;
	private List<Notice> list;
	private int mResource = R.layout.item_notice;
	public NoticeListAdapter(Context context, List<Notice> list) {
		super(context, 0, list);
		// TODO Auto-generated constructor stub
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(context);
		n_context = context;
		this.list = list;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return (list != null) ? list.size() : 0;
	}
	
	@Override
	public View getView (int position, View convertView, final ViewGroup parent) {
		View layout = convertView;
		if (layout == null) {
		    layout = mInflater.inflate( mResource, parent, false);
		}
		final Notice  notice = getItem(position);
		TextView name = (TextView)layout.findViewById(R.id.name);
		name.setText(notice.getAuthorname());
		TextView content = (TextView)layout.findViewById(R.id.messege_content);
		content.setText(notice.getNote()); 
		ImageView avatar = (ImageView)layout.findViewById(R.id.avatar);
		ImageView verity_type = (ImageView)layout.findViewById(R.id.verify_type);
		verity_type.setBackgroundResource(DataService.getVerifyTypeRes(notice.getAuthorverifyType()));
		imageLoader.DisplayImage(notice.getAuthoravatar(), avatar, 0, 0, 0);
		final View hadRead = (View)layout.findViewById(R.id.tv_hadRead);
		if (notice.getIsnew().equals("1")) {
			hadRead.setVisibility(View.VISIBLE);
		} else {
			hadRead.setVisibility(View.GONE);
		}
		layout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				hadRead.setVisibility(View.GONE);
				if (notice.getType().equals("friend")) {
					Intent intent = new Intent(n_context, SpaceActivity.class);
					intent.putExtra("uid", notice.getAuthoruid());
					n_context.startActivity(intent);
				} else if (notice.getType().equals("talkcomment") || notice.getType().equals("forward")) {
					Intent intent = new Intent(n_context, ContentActivity.class);
					intent.putExtra("id", notice.getSid());
					intent.putExtra("idtype", "talkid");
				//	intent.putExtra("pushType", "notifce");
					intent.putExtra("isFromNotice", true);
					n_context.startActivity(intent);
				} else {
					Intent intent = new Intent(n_context,IndexActivity.class);
					n_context.startActivity(intent);
				}
				
				
			}
		});
		return layout;
	}
}
